package com.frame.filter

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView

import javax.servlet.Filter
import javax.servlet.FilterChain
import javax.servlet.FilterConfig
import javax.servlet.ServletException
import javax.servlet.ServletRequest
import javax.servlet.ServletResponse
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.ws.rs.container.ContainerRequestContext
import javax.ws.rs.container.ContainerResponseContext
import javax.ws.rs.container.ContainerResponseFilter

/**
 * 跨域访问设置过滤器
 *
 * Created by three on 14-7-29.
 * com.app.filter
 * User: 王利平
 * Date: 2014/7/29
 */
class CorsResponseFilter implements ContainerResponseFilter,HandlerInterceptor,Filter {

    private Logger log = LoggerFactory.getLogger(CorsResponseFilter.class);

    @Override
    void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {

        def method = request.method;
        response.getHeaders().putSingle("Access-Control-Allow-Origin", "http://127.0.0.1:9000")
        response.getHeaders().putSingle("Access-Control-Allow-Credentials", "true")
        response.getHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
        response.getHeaders().putSingle("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'")
        switch (method){
            case "OPTIONS" :

                response.getHeaders().putSingle("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                response.getHeaders().putSingle("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range")

                break;
            case 'POST':
            case "PUT":
            case "DELETE":
                response.getHeaders().putSingle("Access-Control-Allow-Headers", "Cookie,Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                break;
            case "GET":
                response.getHeaders().putSingle("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
                response.getHeaders().putSingle("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range");
                break;
            case "HEAD":
                break;
        }
    }

    @Override
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true
    }

    @Override
    void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        def method = request.method;
        response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:9000")
        response.addHeader("Access-Control-Allow-Credentials", "true")
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
        response.addHeader("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'")
        switch (method){
            case "OPTIONS" :

                response.addHeader("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                response.addHeader("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range")

                break;
            case 'POST':
            case "PUT":
            case "DELETE":
                response.addHeader("Access-Control-Allow-Headers", "Cookie,Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                break;
            case "GET":
                response.addHeader("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
                response.addHeader("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range");
                break;
            case "HEAD":
                break;
        }
    }

    @Override
    void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        ServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String method = request.method;
        response.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:9000")
        response.addHeader("Access-Control-Allow-Credentials", "true")
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
        response.addHeader("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'")
        switch (method){
            case "OPTIONS" :

                response.addHeader("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                response.addHeader("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range")

                break;
            case 'POST':
            case "PUT":
            case "DELETE":
                response.addHeader("Access-Control-Allow-Headers", "Cookie,Authorization,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type")
                break;
            case "GET":
                response.addHeader("Access-Control-Allow-Headers", "Cookie,Authorization,Range,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type");
                response.addHeader("Access-Control-Expose-Headers", "Accept-Ranges,Content-Encoding,Content-Length,Content-Range");
                break;
            case "HEAD":
                break;
        }
        chain.doFilter(servletRequest, servletResponse);
    }

    @Override
    void destroy() {

    }
}
